<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<link rel="stylesheet" href="css/mui.min.css">
		<link rel="stylesheet" href="css/icons-extra.css" />
		<style>
			[v-cloak] {
				display: none;
			}
			
			html,
			body {
				width: 100%;
				height: 100%
			}
			
			.mui-content {
				padding: 0 20px;
				padding-top: 100px!important;
				background-color: initial;
			}
			
			.bg {
				background: url(./images/login-bg.png);
				background-size: 100%;
				background-repeat: no-repeat;
			}
			
			.login-bg {
				background-image: url(./images/login-input-bg.png);
				background-size: 100% 100%;
				background-repeat: no-repeat;
				min-height: 240px;
				max-width: 334px;
				padding: 106px 20px 0px;
				margin: 0 auto;
			}
			
			.form-row input {
				border: 0px;
				background-color: transparent;
				background-image: url(./images/input-bg.png);
				background-size: 100% 100%;
				background-repeat: no-repeat;
				padding-left: 20%;
				color: #27CDDB;
			}
			
			input::-webkit-input-placeholder,
			input::placeholder {
				color: #27CDDB;
				font-size: 12px;
			}
			
			.login-btn {
				padding: 10px 0px;
				text-align: center;
			}
			
			.login-btn button {
				border: 0px;
				background-color: transparent;
				background-image: url(./images/input-bg.png);
				background-size: 100% 100%;
				background-repeat: no-repeat;
				color: #27CDDB;
				height: 40px;
				outline: none;
				border-radius: 20px;
				margin: 0px;
				max-width: 334px;
			}
			
			.reg {
				margin-top: 10px;
				padding: 0px 20px;
			}
			
			.reg a,
			.reg a:active {
				color: #25CDDA;
				font-size: 12px;
			}
		</style>
	</head>

	<body style="height: 100%;width:100%;background: url(images/login-bg.png);background-repeat: no-repeat;background-size: 100% 100%;">
		<div class="mui-content" id="aicVue" v-cloak>
			<div class="login-bg">
				<div class="form-row">
					<input type="number" placeholder="手机号" v-model="form.mobile">
				</div>
				<div class="form-row" style="margin-top: 3px;">
					<input type="password" placeholder="密码" v-model="form.password">
				</div>
			</div>
			<div class="login-btn">
				<button type="button" style="width: 100%;" @click="validateForm">登陆</button>
				<button type="button" id="TencentCaptcha" data-appid="2044550976" data-cbfn="vm.login" style="display: none;">登陆</button>
			</div>
			<div class="reg">
				<a href="regist/regist.html" style="float: right;">
					点击注册
				</a>
				<a href="login/forget-password.html">
					忘记密码？
				</a>
			</div>
		</div>
		<script type="text/javascript" src="js/mui.min.js"></script>
		<script type="text/javascript" src="js/vue.min.js"></script>
		<script type="text/javascript" src="js/validator.min.js"></script>
		<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
		<script type="text/javascript" src="js/public.js"></script>
		<script>
			mui.init();
			var vm = new Vue({
				el: '#aicVue',
				data: {
					form: {
						mobile: "",
						password: ""
					}
				},
				mounted: function() {
					mui.plusReady(function() {
						//读取本地存储，检查是否为首次启动
						var showGuide = plus.storage.getItem("lauchFlag");
						if(showGuide) {
							vm.startup();
						} else {
							//显示启动导航
							mui.openWindow({
								id: 'guide',
								url: 'login/guide.html',
								styles: {
									popGesture: "none"
								},
								show: {
									aniShow: 'none'
								},
								waiting: {
									autoShow: false
								}
							});
							//延迟的原因：优先打开启动导航页面，避免资源争夺
							setTimeout(function() {
								vm.startup();
							}, 100);
						}
					});
				},
				methods: {
					startup: function() {
						//config init
						mui.postJSON(ConfigInit, {}, function(result) {
							mConsole.log("获取配置信息：" + JSON.stringify(result));
							if(result.code == 0) {
								plus.storage.setItem("config", JSON.stringify(result));
								$("#TencentCaptcha").attr("data-appid", result.tencentAid);
								$.getScript("https://ssl.captcha.qq.com/TCaptcha.js");
							}
						});

						//resetToken
						mui.postJSON(Rettoken, {}, function(result) {
							mConsole.log("重置tooken：" + JSON.stringify(result));
							if(result.code == 0) {
								plus.storage.setItem("tooken", result.token);
								mui.preload({
									url: "main/main.html",
									id: "main"
								});
								vm.checkPreload();
								return;
							}
							plus.navigator.closeSplashscreen(); //关闭启动界面
						});

						//检查更新
						plus.io.resolveLocalFileSystemURL("_doc/update/", function(entry) {
							if(entry.isDirectory) {
								//删除历史下载版本
								entry.removeRecursively(function() {
									vm.checkUpdate();
								});
							}
						}, function(e) {
							vm.checkUpdate();
						});
					},
					login: function(res) {
						if(res.ret === 0) {
							plus.nativeUI.showWaiting('正在登录', {
								width: '100px',
								height: '100px'
							});
							vm.form.ticket = res.ticket;
							vm.form.randstr = res.randstr;
							mui.postJSON(LoginUrl, vm.form, function(result) {
								plus.nativeUI.closeWaiting();
								if(result.code == 0) {
									vm.form.password = ""; //密码置空
									plus.storage.setItem("tooken", result.token);
									mui.openWindow("main/main.html", "main");
									return;
								}
								mymui(result.msg);
							});
						}
					},
					validateForm: function() {
						if(!validator.isMobilePhone(vm.form.mobile, 'zh-CN')) {
							mymui("请输入正确手机号码");
							return;
						}
						if(validator.isEmpty(vm.form.password)) {
							mymui("请输入密码");
							return;
						}
						document.getElementById("TencentCaptcha").click();
					},
					checkPreload: function() {
						var array = plus.webview.all();
						if(array) {
							for(var i = 0, len = array.length; i < len; i++) {
								if(array[i].getURL().endWith("main.html")) {
									plus.webview.getWebviewById("main").show();
									plus.navigator.closeSplashscreen(); //关闭启动界面
									return;
								}
							}
						}
						setTimeout(vm.checkPreload, 500);
					},
					checkUpdate: function() {
						//检查更新
						plus.runtime.getProperty(plus.runtime.appid, function(inf) {
							mConsole.log("当前应用版本：" + inf.version);
							mui.postParam(CheckUpdate, {
								curVersion: inf.version
							}, function(result) {
								if(result.code == 0) {
									if(result.update) {
										plus.downloader.createDownload(result.url, {
											filename: "_doc/update/"
										}, function(d, status) {
											if(status == 200) {
												mConsole.log("下载更新包成功：" + d.filename);
												mConsole.log("安装更新包...");
												plus.runtime.install(d.filename, {force: true}, function() {
													mConsole.log("安装更新包成功！");
												}, function(e) {
													mConsole.log("安装更新包失败[" + e.code + "]：" + e.message);
												});
											} else {
												mConsole.log("下载更新包失败！");
											}
										}).start();
									}
								}
							});
						});
					}
				}
			});
		</script>
	</body>

</html>